angular.module('GetStock')

.controller('GetStock.HomeCtrl', ['$scope', '$state', 'GetStockHomeService', 'User', 'imanDialog', '$ionicLoading', function($scope, $state, GetStockHomeService, User, imanDialog, $ionicLoading) {
    $scope.data = {};
    var barcodeMasters = [];
    var listData = [];

    $scope.homeData = {
        connectAreaName: "", //交接区名字
        isdisabled: false //送货按钮是否可用
    };

    $scope.data.gridOptions = {
        enableRefresh: true,

        columnDefs: [{
                col: 'Priority',
                name: '优先级',
                cls: 'getstock-column-underline'
            }, {
                col: 'NOLotNumber',
                name: '批次'
            }, {
                col: 'ItemMasterCode',
                name: '料号'
            }, {
                col: 'ItemMasterName',
                name: '品名'
            }, {
                col: 'RcvQty',
                name: '上游库存',
                cls: 'getstock-column-red'
            }, {
                col: 'TotalReceiveQty',
                name: '到货数量'
            }, {
                col: 'NeedDeliveryQty',
                name: '待配送数量',
                cls: 'getstock-column-green'
            }, {
                col: 'DemandQty',
                name: '要货数量',
                cls: 'demo-demand-column',
                tpls: '<im-number-input class="col im-record-column demo-demand" number="gridOptions.data[%d].DemandQty"></im-number-input>'
            }


        ],
        data: []
    };

    function initData() {
        $ionicLoading.show({
            template: 'Loading...'
        });
        GetStockHomeService.getConnectArea("KB0000001").then(function(data) {
            $scope.homeData.connectAreaName = data.Name;
            var deliveryMode = data.DeliveryMode;
            var pushPullMode = data.PushPullMode;

            GetStockHomeService.getGetStockData(User.get("_id")).then(function(data) {
                $ionicLoading.hide();
                if (data == null || data.length === 0) {
                    imanDialog.alert('获取看板数据为空', '');
                } else {
                    barcodeMasters = data;
                    for (var i = 1; i <= 20; i++) {
                        barcodeMasters.push({
                            Priority: 50 + i,
                            NOLotNumber: 'P2012',
                            ItemMasterCode: 'ST021',
                            ItemMasterName: '网拍',
                            RcvQty: 200,
                            TotalReceiveQty: 100,
                            NeedDeliveryQty: 0,
                            DemandQty: 0
                        });
                    }
                    $scope.data.gridOptions.data = barcodeMasters;
                    listData = $scope.data.gridOptions.data;
                }

            }, function(error) {
                $ionicLoading.hide();
                imanDialog.alert('获取看板数据失败', '');
                for (var i = 1; i <= 20; i++) {
                    barcodeMasters.push({
                        Priority: 50 + i,
                        NOLotNumber: 'P2012',
                        ItemMasterCode: 'ST021',
                        ItemMasterName: '网拍',
                        RcvQty: 200,
                        TotalReceiveQty: 100,
                        NeedDeliveryQty: 0,
                        DemandQty: 0
                    });
                }
                $scope.data.gridOptions.data = barcodeMasters;
                listData = $scope.data.gridOptions.data;
            });
            //交接区.配送模式 = 0（物流小车） and 交接区.推拉模式 = 1（拉式）
            // and 画面上存在待配送量>0的记录，则送货按钮可用，否则不可用。
            var isNeedDeliveryQty = false; //是否待配送量>0
            for (var i = 0; i < listData.length; i++) {
                if (listData[i].NeedDeliveryQty > 0) {
                    isNeedDeliveryQty = true;
                    break;
                }
            }
            if (deliveryMode === 0 && pushPullMode === 1 && isNeedDeliveryQty) {
                $scope.homeData.isdisabled = false; //可用
            } else {
                $scope.homeData.isdisabled = true; //不可用
            }

        }, function(error) {
            $ionicLoading.hide();
            imanDialog.alert('获取交接区失败', '');

        });

    }
    initData();


    $scope.search = function(query) {

        if (query === "") {
            $scope.data.gridOptions.data = listData;
        }
        if (query !== "") {
            var nPosMo;
            var nPosItem;
            $scope.data.gridOptions.data = [];

            for (var i in listData) {
                var noLotNumber = listData[i].NOLotNumber;
                var itemMasterCode = listData[i].ItemMasterCode;
                nPosMo = find(query, noLotNumber);
                nPosItem = find(query, itemMasterCode);
                //nPos=sTxt.indexOf(sFind);   
                if (nPosMo >= 0 || nPosItem >= 0) {

                    $scope.data.gridOptions.data.push(listData[i]);
                }
            }
            if ($scope.data.gridOptions.data.length === 0) {
                imanDialog.alert('未找到相关信息', '');
            }
        }

    };

    function find(sFind, sObj) {
        var nSize = sFind.length;
        var nLen = sObj.length;
        var sCompare;

        if (nSize <= nLen) {
            for (var i = 0; i <= nLen - nSize + 1; i++) {
                sCompare = sObj.substring(i, i + nSize);
                if (sCompare === sFind) {
                    return i;
                }
            }
        }
        return -1;
    }


    $scope.doRefresh = function(operate, item) {
        $scope.data.gridOptions.data.unshift({
            Priority: 100,
            NOLotNumber: 'P2016',
            ItemMasterCode: 'SA006',
            ItemMasterName: '新网拍',
            RcvQty: 200,
            TotalReceiveQty: 100,
            NeedDeliveryQty: 0,
            DemandQty: 0
        });
        $scope.$broadcast('scroll.refreshComplete');
    };
}]);